//package com.alibaba.cirtus.ots.application.adapter.ability.facade.service.dp.mndataif002;
//
//import com.alibaba.cirtus.ots.application.adapter.ability.model.features.CustomerAddressFeatures;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.features.CustomerFeatures;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.features.CustomerSaleScopeFeatures;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerAddressSDO;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerSDO;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerSaleScopeSDO;
//import com.alibaba.cz.base.tool.log.Log;
//import com.alibaba.fastjson.JSONObject;
//import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByCodes2Request;
//import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByCodes2Response;
//import com.epoch.app.bcots.model.dto.OrganizationSDO;
//import com.epoch.app.bcots.service.BaseDataService;
//import com.epoch.app.otsapplicationadaptersdk.dp.DpCustmer;
//import com.google.common.collect.Lists;
//import com.google.common.collect.Sets;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.jetbrains.annotations.Nullable;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * @author wangyibing
// */
//@Component
//public class MnDataIf002SDOBuilder {
//    Log log = Log.getLogger(MnDataIf002SDOBuilder.class);
//    @Resource
//    private BaseDataService baseDataService;
//
//    public CustomerSDO buildCustomerMainDataSDO(DpCustmer dpCustomer, Map<String, OrganizationSDO> organizationMapByCustomers) {
//        if (!checkCustomerData(dpCustomer, organizationMapByCustomers)) {
//            return null;
//        }
//        // 2、客户主数据字段映射
//        CustomerSDO customerSDO = new CustomerSDO();
//        customerSDO.setName(dpCustomer.getName1());
//        customerSDO.setCode(dpCustomer.getKunnr());
//        customerSDO.setOutCode(dpCustomer.getKunnr());
//        customerSDO.setType(dpCustomer.getKtokd());
//        customerSDO.setGroup(dpCustomer.getKdgrp());
//        // 省市区 名称
//        customerSDO.setProvince(dpCustomer.getRegiodesc());
//        customerSDO.setCity(dpCustomer.getZzcityt());
//        customerSDO.setArea(dpCustomer.getZzregit());
//
//        customerSDO.setUnifiedSocialCreditCode(dpCustomer.getZzcus002());
//        // 注册地省市区 编码
//        customerSDO.setRegistrationProvinceCode(dpCustomer.getZzprov());
//        customerSDO.setRegistrationCityCode(dpCustomer.getZzcity());
//        customerSDO.setRegistrationAreaCode(dpCustomer.getZzregi());
//        // 法人
//        customerSDO.setLegalPerson(dpCustomer.getZzcus014());
//        // 是否一般纳税人
//        customerSDO.setIsTaxpayer(dpCustomer.getZzcus012());
//        customerSDO.setOutCreateDate(dpCustomer.getErdat());
//        // 状态
//        if (StringUtils.isNotBlank(dpCustomer.getAufsd())) {
//            customerSDO.setStatus(0);
//        } else {
//            customerSDO.setStatus(1);
//        }
//        // features:注册地省市区名称
//        CustomerFeatures customerFeatures = new CustomerFeatures();
//        customerFeatures.setRegisterProvinceName(dpCustomer.getZzprovt());
//        customerFeatures.setRegisterCityName(dpCustomer.getZzcityt());
//        customerFeatures.setRegisterRegioName(dpCustomer.getZzregit());
//        String features = com.alibaba.fastjson.JSONObject.toJSONString(customerFeatures);
//        customerSDO.setFeatures(features);
//        return customerSDO;
//    }
//
//    public CustomerSaleScopeSDO buildCustomerSaleScopeSDO(DpCustmer dpCustomer, Map<String, OrganizationSDO> organizationMapByCustomers) {
//        if (!"11".equals(dpCustomer.getSpart())) {
//            return null;
//        }
//        //同步销售范围
//        CustomerSaleScopeSDO customerSaleScopeSDO = new CustomerSaleScopeSDO();
//        if (!organizationMapByCustomers.containsKey(dpCustomer.getVkorg())) {
//            log.error("销售组织映射不上,kunnr:{},vkorg:{}", dpCustomer.getKunnr(), dpCustomer.getVkorg());
//            return null;
//        }
//        OrganizationSDO saleOrgSDO = organizationMapByCustomers.get(dpCustomer.getVkorg());
//        String saleOrg = saleOrgSDO.getId();
//        String business = saleOrgSDO.getParentId();
//        customerSaleScopeSDO.setOrgSaleOrganizationCode(saleOrg);
//        customerSaleScopeSDO.setOrgBussinessUnitCode(business);
//        if (StringUtils.isNotBlank(dpCustomer.getVkbur()) && organizationMapByCustomers.containsKey(dpCustomer.getVkbur())) {
//            OrganizationSDO channelSDO = organizationMapByCustomers.get(dpCustomer.getVkbur());
//            String saleChannel = channelSDO.getId();
//            customerSaleScopeSDO.setOrgSaleChannelCode(saleChannel);
//        } else {
//            if (StringUtils.isNotBlank(dpCustomer.getVkbur())) {
//                log.error("销售渠道映射不上,kunnr:{},vkbur:{}", dpCustomer.getKunnr(), dpCustomer.getVkbur());
//                return null;
//            }
//        }
//
//        if (StringUtils.isNotBlank(dpCustomer.getVkgrp()) && organizationMapByCustomers.containsKey(dpCustomer.getVkgrp())) {
//            OrganizationSDO departSDO = organizationMapByCustomers.get(dpCustomer.getVkgrp());
//            String department = departSDO.getId();
//            customerSaleScopeSDO.setOrgSaleDepartmentCode(department);
//        } else {
//            if (StringUtils.isNotBlank(dpCustomer.getVkgrp())) {
//                log.error("销售部门映射不上,kunnr:{},vkgrp:{}", dpCustomer.getKunnr(), dpCustomer.getVkgrp());
//                return null;
//            }
//        }
//        customerSaleScopeSDO.setCustomerCode(dpCustomer.getKunnr());
//        customerSaleScopeSDO.setSalesArea(dpCustomer.getBzirk());
//        customerSaleScopeSDO.setItemGroup(dpCustomer.getSpart());
//        customerSaleScopeSDO.setChannelCode(dpCustomer.getVtweg());
//        customerSaleScopeSDO.setGroup(dpCustomer.getKtokd());
//        customerSaleScopeSDO.setOutCreateDate(dpCustomer.getErdat());
//        if (StringUtils.isNotBlank(dpCustomer.getAufsd())) {
//            customerSaleScopeSDO.setStatus(0);
//        } else {
//            customerSaleScopeSDO.setStatus(1);
//        }
//
//        // features:货品组名称、渠道名称、运输区域编码/名称、销售区域名称
//        CustomerSaleScopeFeatures customerSaleScopeFeatures = new CustomerSaleScopeFeatures();
//        customerSaleScopeFeatures.setItemGroupName(dpCustomer.getSpartdesc());
//        customerSaleScopeFeatures.setChannelName(dpCustomer.getVtwegdesc());
//        customerSaleScopeFeatures.setTransportArea(dpCustomer.getLzone());
//        customerSaleScopeFeatures.setTransportAreaName(dpCustomer.getLzoneesc());
//        customerSaleScopeFeatures.setSalesAreaName(dpCustomer.getBzirkdesc());
//        customerSaleScopeFeatures.setOrgSalesOrganizationName(dpCustomer.getVkorgdesc());
//        customerSaleScopeFeatures.setOrgSalesChannelName(dpCustomer.getVkburdesc());
//        customerSaleScopeFeatures.setOrgSalesDepartmentName(dpCustomer.getVkgrpdesc());
//        customerSaleScopeFeatures.setOutOrgSalesDepartmentCode(dpCustomer.getVkgrp());
//        customerSaleScopeFeatures.setOutOrgSalesDepartmentName(dpCustomer.getVkgrpdesc());
//        customerSaleScopeFeatures.setOutOrgSalesChannelCode(dpCustomer.getVkbur());
//        customerSaleScopeFeatures.setOutOrgSalesChannelName(dpCustomer.getVkburdesc());
//        customerSaleScopeFeatures.setStoreHouse(dpCustomer.getVwerk());
//        customerSaleScopeFeatures.setStoreHouseName(dpCustomer.getVwerkdesc());
//        customerSaleScopeFeatures.setContactName(dpCustomer.getZzcus901());
//        customerSaleScopeFeatures.setCellphoneNumber(dpCustomer.getTelf1());
//        customerSaleScopeFeatures.setAddress(dpCustomer.getStreet());
//        String scopeFeatures = JSONObject.toJSONString(customerSaleScopeFeatures);
//        customerSaleScopeSDO.setFeatures(scopeFeatures);
//        return customerSaleScopeSDO;
//    }
//
//    public CustomerAddressSDO buildCustomerAddressSdo(DpCustmer dpCustomer, Map<String, OrganizationSDO> organizationMapByCustomers) {
//        if (!"11".equals(dpCustomer.getSpart())) {
//            return null;
//        }
//        //同步售达方收货地址
//        CustomerAddressSDO customerAddressSDO = new CustomerAddressSDO();
//
//        if (!organizationMapByCustomers.containsKey(dpCustomer.getVkorg())) {
//            log.error("销售组织映射不上,kunnr:{},vkorg:{}", dpCustomer.getKunnr(), dpCustomer.getVkorg());
//            return null;
//        }
//        OrganizationSDO saleOrgSDO = organizationMapByCustomers.get(dpCustomer.getVkorg());
//        String saleOrg = saleOrgSDO.getId();
//        String business = saleOrgSDO.getParentId();
//        customerAddressSDO.setOrgSaleOrganizationCode(saleOrg);
//        customerAddressSDO.setOrgBussinessUnitCode(business);
//        if (StringUtils.isNotBlank(dpCustomer.getVkbur()) && organizationMapByCustomers.containsKey(dpCustomer.getVkbur())) {
//            OrganizationSDO channelSDO = organizationMapByCustomers.get(dpCustomer.getVkbur());
//            String saleChannel = channelSDO.getId();
//            customerAddressSDO.setOrgSaleChannelCode(saleChannel);
//        } else {
//            if (StringUtils.isNotBlank(dpCustomer.getVkbur())) {
//                log.error("销售渠道映射不上,kunnr:{},vkbur:{}", dpCustomer.getKunnr(), dpCustomer.getVkbur());
//                return null;
//            }
//        }
//
//        if (StringUtils.isNotBlank(dpCustomer.getVkgrp()) && organizationMapByCustomers.containsKey(dpCustomer.getVkgrp())) {
//            OrganizationSDO departSDO = organizationMapByCustomers.get(dpCustomer.getVkgrp());
//            String department = departSDO.getId();
//            customerAddressSDO.setOrgSaleDepartmentCode(department);
//        } else {
//            if (StringUtils.isNotBlank(dpCustomer.getVkgrp())) {
//                log.error("销售部门映射不上,kunnr:{},vkgrp:{}", dpCustomer.getKunnr(), dpCustomer.getVkgrp());
//                return null;
//            }
//        }
//
//        customerAddressSDO.setCustomerAccountGroup(dpCustomer.getKtokd());
//        //客户编码、地址编码
//        customerAddressSDO.setCustomerCode(dpCustomer.getKunnr());
//        customerAddressSDO.setOutCustomerCode(dpCustomer.getKunnr());
//        customerAddressSDO.setAddressCode(dpCustomer.getKunnr());
//        customerAddressSDO.setOutCode(dpCustomer.getKunnr());
//        //收获地址名称、详细地址、联系电话、联系人
//        customerAddressSDO.setAddressName(dpCustomer.getName1() + "默认地址");
//        customerAddressSDO.setAddress(dpCustomer.getStreet());
//        customerAddressSDO.setContactName(dpCustomer.getZzcus901());
//        customerAddressSDO.setCellphoneNumber(dpCustomer.getTelf1());
//        //销售地区
//        customerAddressSDO.setSalesArea(dpCustomer.getBzirk());
//        customerAddressSDO.setChannelCode(dpCustomer.getVtweg());
//        customerAddressSDO.setItemGroup(dpCustomer.getSpart());
//        customerAddressSDO.setType(1);
//        customerAddressSDO.setOutCreateDate(dpCustomer.getErdat());
//        if (StringUtils.isNotBlank(dpCustomer.getAufsd())) {
//            customerAddressSDO.setStatus(0);
//        } else {
//            customerAddressSDO.setStatus(1);
//        }
//
//        CustomerAddressFeatures customerAddressFeatures = new CustomerAddressFeatures();
//        customerAddressFeatures.setCustomerGroup(dpCustomer.getKdgrp());
//        //渠道、货品组名称
//        customerAddressFeatures.setOrgSalesOrganizationName(dpCustomer.getVkorgdesc());
//        customerAddressFeatures.setOrgSalesChannelName(dpCustomer.getVkburdesc());
//        customerAddressFeatures.setOrgSalesDepartmentName(dpCustomer.getVkgrpdesc());
//
//        //装运条件
//        customerAddressFeatures.setShippingConditions(dpCustomer.getVsbed());
//        customerAddressFeatures.setShippingConditionName(dpCustomer.getVsbeddesc());
//        //仓库
//        customerAddressFeatures.setStoreHouse(dpCustomer.getVwerk());
//        //仓库名称
//        customerAddressFeatures.setStoreHouseName(dpCustomer.getVwerkdesc());
//        //冻结状态
//        customerAddressFeatures.setFrozenStatus(dpCustomer.getAufsd());
//        // 运输区域
//        customerAddressFeatures.setTransportArea(dpCustomer.getLzone());
//        customerAddressFeatures.setTransportAreaName(dpCustomer.getLzoneesc());
//        customerAddressFeatures.setRegio(dpCustomer.getRegiodesc());
//        customerAddressFeatures.setOutOrgSalesDepartmentCode(dpCustomer.getVkgrp());
//        customerAddressFeatures.setOutOrgSalesDepartmentName(dpCustomer.getVkgrpdesc());
//        customerAddressFeatures.setOutOrgSalesChannelCode(dpCustomer.getVkbur());
//        customerAddressFeatures.setOutOrgSalesChannelName(dpCustomer.getVkburdesc());
//        customerAddressFeatures.setSalesAreaName(dpCustomer.getBzirkdesc());
////        //伙伴功能   售达方没有
////        customerAddressFeatures.setParvw("");
////        //业务伙伴的客户编号  售达方没有
//        customerAddressFeatures.setKunn2(dpCustomer.getKunnr());
//
//        String addressFeatures = JSONObject.toJSONString(customerAddressFeatures);
//        customerAddressSDO.setFeatures(addressFeatures);
//        return customerAddressSDO;
//    }
//
//    @Nullable
//    private Map<String, OrganizationSDO> getOrganizationSDO(List<String> orgCodes) {
//        BaselineGetOrganizationByCodes2Request byCodes2Request = BaselineGetOrganizationByCodes2Request.builder().ids(orgCodes).status(1).build();
//        BaselineGetOrganizationByCodes2Response byCodes2Response = baseDataService.baselineGetOrganizationByCodes2(byCodes2Request);
//        if (Objects.isNull(byCodes2Response) || CollectionUtils.isEmpty(byCodes2Response.getResult())) {
//            return null;
//        }
//        return byCodes2Response.getResult().stream().collect(Collectors.toMap(OrganizationSDO::getCode, obj -> obj));
//    }
//
//    @Nullable
//    public Map<String, OrganizationSDO> getOrganizationMapByCustomers(List<DpCustmer> dpCustomers) {
//        Set<String> vkOrgSet = dpCustomers.stream().map(DpCustmer::getVkorg).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
//        Set<String> vkBurSet = dpCustomers.stream().map(DpCustmer::getVkbur).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
//        Set<String> vkGrpSet = dpCustomers.stream().map(DpCustmer::getVkgrp).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
//        Set<String> orgCodes = Sets.newHashSet(vkOrgSet);
//        orgCodes.addAll(vkBurSet);
//        orgCodes.addAll(vkGrpSet);
//        BaselineGetOrganizationByCodes2Request byCodes2Request = BaselineGetOrganizationByCodes2Request.builder().ids(Lists.newArrayList(orgCodes)).status(1).build();
//        BaselineGetOrganizationByCodes2Response byCodes2Response = baseDataService.baselineGetOrganizationByCodes2(byCodes2Request);
//        if (Objects.isNull(byCodes2Response) || CollectionUtils.isEmpty(byCodes2Response.getResult())) {
//            log.error("所有组织都映射不上,编码：", orgCodes.toString());
//            return null;
//        }
//        return byCodes2Response.getResult().stream().collect(Collectors.toMap(OrganizationSDO::getCode, a -> a, (a, b) -> a));
//    }
//
//    private boolean checkCustomerData(DpCustmer dpCustomer, Map<String, OrganizationSDO> organizationMapByCustomers) {
//        //查重条件不可为空
//        if (StringUtils.isBlank(dpCustomer.getKunnr()) || StringUtils.isBlank(dpCustomer.getVkorg()) || StringUtils.isBlank(dpCustomer.getSpart()) || StringUtils.isBlank(dpCustomer.getVtweg())) {
//            log.error("客户唯一条件都不可为空,客户编码:{},销售组织:{},货品组:{},业务渠道:{}", dpCustomer.getVkorg(), dpCustomer.getKunnr(), dpCustomer.getSpart(), dpCustomer.getVtweg());
//            return false;
//        }
//        if (!"11".equals(dpCustomer.getSpart())) {
//            return false;
//        }
//
//        if (!organizationMapByCustomers.containsKey(dpCustomer.getVkorg())) {
//            log.error("销售组织映射不上,kunnr:{},vkorg:{}", dpCustomer.getKunnr(), dpCustomer.getVkorg());
//            return false;
//        }
//
//        //除电商外其它售达方 销售渠道和销售部门必须有映射
//        if (StringUtils.isNotBlank(dpCustomer.getVkbur()) && !organizationMapByCustomers.containsKey(dpCustomer.getVkbur())) {
//            log.error("销售渠道映射不上,kunnr:{},vkbur:{}", dpCustomer.getKunnr(), dpCustomer.getVkbur());
//            return false;
//        }
//
//        if (StringUtils.isNotBlank(dpCustomer.getVkgrp()) && !organizationMapByCustomers.containsKey(dpCustomer.getVkgrp())) {
//            log.error("销售部门映射不上,kunnr:{},vkgrp:{}", dpCustomer.getKunnr(), dpCustomer.getVkgrp());
//            return false;
//        }
//        return true;
//    }
//
//
//}
